from rest_framework.viewsets import ModelViewSet
from goods.models import SKUImage
from meiduo_admin.my_paginate import MyPageNuberPagination
from meiduo_admin.image.image_serializers import ImageViewserializer,SPUSerializer
from goods.models import SKU
from rest_framework.response import Response
from fdfs_client.client import Fdfs_client
from django.conf import settings

#图片管理
class ImageViewSet(ModelViewSet):
    pagination_class = MyPageNuberPagination
    serializer_class = ImageViewserializer
    queryset = SKUImage.objects.all()

    # 1.新增图片
    def simple(self,request):

        # 1,查询所有的sku对象
        skus=SKU.objects.all()
        # 2,获取序列化器
        serializer=SPUSerializer(instance=skus,many=True)

        ##3,返回响应
        return Response(serializer.data)

    # 2,重写create方法,保存新增图片数据
    def create(self, request, *args, **kwargs):
        ##1,获取参数
        image=request.FILES.get("image")
        sku_id=request.data.get("sku")

        # 2,校验参数
        if not all([image,sku_id]):
            return Response(status=403)

        # 3,数据入库到storage容器的fdfs
        client=Fdfs_client(settings.FDFS_CONFIG)
        ret=client.upload_by_buffer(image.read())

        # 3,1判断是否成功
        if ret["Status"] != "Upload successed.":
            return Response(status=400)

        # 3,2数据入库图片
        image_url=ret.get("Remote file_id")
        SKUImage.objects.create(sku_id=sku_id,image=image_url)
        # 如果第二次没有上传图片默认给他第一张图片
        SKU.objects.filter(id=sku_id,default_image_url="").update(default_image_url=image_url)
        # 4,返回响应
        return Response(status=201)

    #图片表修改,重写update方法,修改图片
    def update(self, request, *args, **kwargs):
        image=request.FILES.get("image")
        sku_id=request.data.get("sku")
        sku_image=self.get_object()

        #2.校验参数
        if not all([image,sku_id]):
            return Response(status=400)
        # 3,数据入库
        client=Fdfs_client(settings.FDFS_CONFIG)
        ret=client.upload_by_buffer(image.read())

        #3.1判断是否成功
        if ret["Status"] != ["Upload successed."]:
            return Response(status=400)
        # 3,2数据入库图片
        image_url=ret.get("Remote file_id")
        SKUImage.objects.filter(id=sku_image.id).update_or_create(sku_id=sku_id,image=image_url)
        # 4,返回响应
        return Response(status=201)


